﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Qiao.Common.Core
{
    /// <summary>
    /// 集合扩展
    /// </summary>
    public static class ListExtend
    {

        #region 集合是否满足指定条件条件
        /// <summary>
        /// 集合是否满足指定条件条件
        /// </summary>
        /// <typeparam name="TSource"></typeparam>
        /// <param name="source">数据源</param>
        /// <param name="conditions">条件</param>
        /// <returns></returns>
        public static bool IsListSatisfactionConditions<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> conditions)
        {
            return source.Any(conditions);
        }
        #endregion

        #region 集合转字符串
        /// <summary>
        /// 集合转字符串
        /// </summary>
        /// <typeparam name="TSource"></typeparam>
        /// <param name="source">数据源</param>
        /// <param name="Symbol">分割符</param>
        /// <returns></returns>
        public static string ListToString<TSource>(this IEnumerable<TSource> source, string Symbol)
        {
            string str = source.Aggregate(new StringBuilder(), (sb, s) => (sb.Append(s).Append(Symbol)), sb => sb.ToString());
            return str.EndsWith(",") ? str.Substring(0, str.Length - 1) : str;
        }
        #endregion

    }
}
